# 连接数据库

import pymysql
import logging
from typing import Optional, Dict, Any, List
import sys
import os

# 添加项目路径以便导入配置
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

class DatabaseConnection:
    """数据库连接管理类"""
    
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.connection = None
        self._load_config()
    
    def _load_config(self):
        """从配置文件加载数据库配置"""
        try:
            from config.settings import DATABASE_CONFIG
            self.config = DATABASE_CONFIG
        except ImportError:
            # 默认配置（备份）
            self.config = {
                'host': '192.168.0.162',
                'port': 3310,
                'user': 'root',
                'password': '123456',
                'charset': 'utf8mb4',
                'autocommit': True
            }
            self.logger.warning("使用默认数据库配置")
    
    def connect(self, database: str = None) -> bool:
        """连接到数据库"""
        
        pass


    def close(self):
        """关闭数据库连接"""
        if self.connection:
            self.connection.close()
            self.connection = None
            self.logger.info("数据库连接已关闭")

# 创建全局数据库连接实例
db_connection = DatabaseConnection()

def get_db_connection() -> DatabaseConnection:
    """获取数据库连接实例（单例）"""
    return db_connection